<div class="layui-fluid nepadmin-pad0 nepadmin-mar0 list0 clientlist" id="list" lay-title="任务管理">
	<div class="layui-fluid">
		<div class="layui-row layui-col-space15">
			<div class="layui-col-md12">
				<div class="layui-card hxr-layui-card" id="userlistv">
					<div class="layui-form layui-card-header hxradmin-card-header">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">关键词查询</label>
								<div class="layui-input-inline">
									<input type="text" name="user" placeholder="任务名称" class="layui-input" autocomplete="off">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">任务状态</label>
								<div class="layui-input-inline">
									<div class="layui-unselect layui-form-select">
										<div class="layui-select-title"><input name="status" type="text" placeholder="请选择状态" value="" readonly class="layui-input layui-unselect"><i
											 class="layui-edge"></i></div>
										<dl class="layui-anim layui-anim-upbit" style="">
											<dd lay-value="" class="layui-select-tips layui-this">请选择状态</dd>
											<dd data='1'>启动</dd>
											<dd data='2'>停用</dd>
										</dl>
									</div>
								</div>
							</div>
							<div class="layui-inline">
								<button class="layui-btn" data-type="reload" id="search">搜索</button>
							</div>
						</div>
					</div>
					<div class="layui-card-body">
						<div>
							<button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button>
							<button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
						</div>
						<table class="layui-hide" id="list-table" lay-filter="list-table"></table>
						<script type="text/html" id="status">
						  <input type="checkbox" name="role_status" lay-skin="switch" lay-text="启用|停用" lay-filter="list-tableF"
						   value="{{ d.status }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.status == '1' ? 'checked' : '' }}>
						</script>
						<script type="text/html" id="protect">
						  <input type="checkbox" name="protect_status" lay-skin="switch" lay-text="启用|停用" lay-filter="list-tableP"
						   value="{{ d.protect }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.protect == '1' ? 'checked' : '' }}>
						</script>
						<div id="list-toolbar" style="display: none;">
						  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
						            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
						</div>
						<script type="text/html" id="type">
						  {{#  if(d.type === 1){ }}
							所有
						  {{#  } else if(d.type === 2) { }}
						    设备组
							{{#  } else if(d.type === 3) { }}
							  设备
						  {{#  } }}
						</script>
						<script type="text/html" id="zip_name">
						  <a download='{{d.file_name}}' href="{{layui.conf.url+d.zip_name}}" class="layui-btn layui-btn-normal layui-btn-xs">下载</a>
						</script>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	var formurl='client/form',formarea=['600px', '600px'];
	//表格数据渲染
	layui.use(['table', 'form','laydate'], function() {
		var table = layui.table,
			form = layui.form,
			$ = layui.$,laydate=layui.laydate;
			layui.admin.initlist(table,'device/task_list',275,[
						[{
							type: 'checkbox',
							fixed:'left'
						}, {
							field: 'task_id',
							title: 'ID',
							width: 60
						},{
							field: 'task_name',
							title: '任务名称',
							minWidth: 100
						},{
							field: 'type',
							title: '类型',
							width: 100,
							templet:'#type'
						}, {
							field: 'file_name',
							title: '执行文件名称',
							minWidth: 200
						}, {
							field: 'unzip_file',
							title: '解压目录',
							minWidth: 200
						}, {
							field: 'size',
							title: '文件大小',
							width: 100
						}, {
							field: 'zip_name',
							title: '文件下载',
							width: 100,
							templet:'#zip_name'
						}, {
							field: 'md5file',
							title: 'md5文件效验值',
							width: 300

						},{
							field: 'execution_time',
							title: '执行时间(秒)',
							width: 110
						}, {
							field: 'additional',
							title: '执行参数',
							width: 300

						}, {
							field: 'device_group_names',
							title: '设备组',
							minWidth: 200,
						}, {
							field: 'device_names',
							title: '设备',
							minWidth: 200,
						},{
							field: 'remarks',
							title: '备注',
							minWidth: 200,
						}, {
							field: 'create_time',
							title: '创建时间',
							width: 200,
						}, {
							field: 'status',
							title: '状态',
							width: 100,
							fixed:'right',
							templet:'#status'
						}, {
							field: 'protect',
							title: '进程守护',
							width: 100,
							fixed:'right',
							templet:'#protect'
						}, {
							title: '操作',
							toolbar: '#list-toolbar',
							width: 150,
							fixed:'right'
						}]
					])
//监听表格表单操作
		form.on('switch(list-tableF)', function(obj) {
			var json = JSON.parse(decodeURIComponent($(this).data('json')));
			json = table.clearCacheKey(json);
			layui.admin.ajax('device/task_update',{
				status:json.status == 1?2:1,
				task_id:json.task_id
			}).then(function(res){
				table.reload('list-table');
			}).catch(function(res){});
		});
		form.on('switch(list-tableP)', function(obj) {
			var json = JSON.parse(decodeURIComponent($(this).data('json')));
			json = table.clearCacheKey(json);
			layui.admin.ajax('device/task_update',{
				protect:json.protect == 1?0:1,
				task_id:json.task_id
			}).then(function(res){
				table.reload('list-table');
			}).catch(function(res){});
		});

		//监听表格操作事件
		table.on('tool(list-table)', function(obj) {
			var data = obj.data;
			//删除
			if (obj.event === 'del') {
				layer.confirm('确定删除' + data.task_name + '?', function(index) {
					layui.admin.delF(table,'device/task_delete',{task_id: data.task_id});
				});
			} else if (obj.event === 'edit') { //编辑

				device_group_ids: ""
				device_ids: ""
				zip_name: "/uploads/task/20200813/ced970d7315c37d85f020dcbedbd5ad1.zip"
				file_name: "111"
				execution_time: 2
				remarks: ""
				status: 1
				create_time: "2020-08-13 16:06:11"
				update_time: "1970-01-01 08:00:00"
				unzip_file: "2"
				additional: ""
				file_size: 7961
				protect: 0
				size: "7.77KB"

				var data_new={
					status:data.stauts,
					task_id:data.task_id,
					file_size:data.file_size,
					file_name:data.file_name,
					device_group_ids:data.device_group_ids,
					execution_time:data.execution_time,
					status:data.status,
					device_ids:data.device_ids,
					unzip_file:data.unzip_file,
					remarks:data.remarks,
					protect:data.protect,
							protect:data.protect,
							type:data.type,
							zip_name:data.zip_name,
							device_group_names:data.device_group_names,
							device_names:data.device_names,
							additional:data.additional,
							task_name:data.task_name,
				};
				layui.admin.pupF('编辑任务',formarea,formurl, 2, data_new);
			}
		});
		//监听行双击事件
		/* table.on('rowDouble(list-table)', function(obj) {
			//查看详情
			if($('.clientlist').length){
				layui.admin.pupF('任务详情', formarea, formurl, 3, obj.data);
			}
		}); */
		var active = {
			//多行删除
			batchdel: function() {
					layui.admin.delsF(table,'device/task_delete','task_id');
				}
				//添加
				,
			add: function(othis) {
				layui.admin.pupF('添加任务', formarea, formurl, 1, {})
			}
		};
		//操作dom
		$('.layui-btn.layuiadmin-btn-list').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
		//搜索
		$('#search').on('click', function() {
			var obj=$(this).parents('.layui-form-item');
			var val1 = obj.find('input[name="user"]').val(),
			val2 = obj.find('input[name="status"]').attr('data');
			layui.admin.reloads({
				search_text:val1,
				status:val2
			});
		});
	});
</script>
